﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Selectbox extends Sprite
    {
        var cwidth:int;
        var cheight:int;
        var scrollbar:Scrollbar;
        public var content:Sprite;
        public var item:SelectItem;

        public function Selectbox(param1:int, param2:int)
        {
            this.cwidth = param1;
            this.cheight = param2;
            this.content = new Sprite();
            this.content.x = 3;
            this.content.y = 1;
            addChild(this.content);
            this.scrollbar = new Scrollbar(this.content, new Rectangle(0, 0, param1, param2 - 2));
            this.scrollbar.y = 1;
            this.scrollbar.x = param1 - 17;
            addChild(this.scrollbar);
            this.Resize(param1, param2);
            return;
        }// end function

        public function Resize(int:int, int:int) : void
        {
            graphics.clear();
            graphics.beginFill(9474192);
            graphics.drawRect(0, 0, int, int);
            graphics.beginFill(16777215);
            graphics.drawRect(1, 1, int - 2, int - 2);
            graphics.beginBitmapFill(new InputBackground());
            graphics.drawRect(1, 1, int - 2, int > 22 ? (20) : (int - 2));
            graphics.endFill();
            this.scrollbar.SetSize(int - 2);
            return;
        }// end function

        public function Set(int:String) : void
        {
            var _loc_2:int = 0;
            if (this.item != null)
            {
                this.item.Select(false);
            }
            while (_loc_2 < this.content.numChildren)
            {
                
                this.item = this.content.getChildAt(_loc_2) as SelectItem;
                if (this.item.name == int)
                {
                    break;
                }
                _loc_2++;
            }
            this.item.Select();
            return;
        }// end function

        public function AddItem(int:String, int:String, int:Boolean = false, int:BitmapData = null) : void
        {
            var _loc_5:* = new SelectItem(int, int, this.cwidth - 4, int);
            _loc_5.addEventListener(MouseEvent.CLICK, this.ItemClick, false, 1, true);
            _loc_5.y = Math.round(this.content.numChildren * (_loc_5.height + 2)) + 2;
            this.content.addChild(_loc_5);
            if (int)
            {
                if (this.item != null)
                {
                    this.item.Select(false);
                }
                this.item = _loc_5;
                _loc_5.Select();
            }
            this.scrollbar.SetContentHeight(this.content.numChildren * (_loc_5.height + 2) + 2);
            return;
        }// end function

        public function RemoveItem() : void
        {
            if (this.item != null)
            {
                this.content.removeChild(this.item);
                this.item = null;
            }
            return;
        }// end function

        public function Rearrange() : void
        {
            var _loc_1:SelectItem = null;
            var _loc_2:int = 0;
            if (this.content.numChildren > 0)
            {
                _loc_2 = 0;
                while (_loc_2 < this.content.numChildren)
                {
                    
                    _loc_1 = this.content.getChildAt(_loc_2) as SelectItem;
                    _loc_1.y = Math.round((_loc_1.height + 2) * _loc_2) + 2;
                    _loc_2++;
                }
                this.scrollbar.SetContentHeight(this.content.numChildren * (_loc_1.height + 2) + 2);
            }
            return;
        }// end function

        private function ItemClick(event:MouseEvent) : void
        {
            if (event.currentTarget != this.item)
            {
                if (this.item != null)
                {
                    this.item.Select(false);
                }
                this.item = event.currentTarget as SelectItem;
                this.item.Select();
                dispatchEvent(new SelectEvent(SelectEvent.COMMAND, this.item.name));
            }
            return;
        }// end function

        public function Clear() : void
        {
            while (this.content.numChildren > 0)
            {
                
                this.content.removeChildAt(0);
            }
            this.scrollbar.SetContentHeight(0);
            return;
        }// end function

        public function get value() : String
        {
            return this.item != null ? (this.item.name) : ("");
        }// end function

    }
}
